package com.coinland.study.window;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.PixelFormat;
import android.graphics.Point;
import android.os.Build;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.coinland.study.R;
import com.coinland.study.bean.DocumentBean;
import com.coinland.study.util.Contant;
import com.coinland.study.util.DisplayUtils;
import com.coinland.study.util.SharedPreUtils;
import com.coinland.study.util.Utils;
import com.coinland.study.window.rom.RomUtils;

import java.util.ArrayList;
import java.util.List;

import static android.content.Context.WINDOW_SERVICE;

/**
 * Created by manji
 * Date：2018/9/29 下午4:29
 * Desc：
 */
public class WindowUtil implements AdapterView.OnItemClickListener,
        View.OnClickListener, DocumentAdapter.IDeleteItem {

    private String TAG = WindowUtil.class.getSimpleName();

    private WindowManager mWindowManager;

    private IWinCallback iWinCallback;

    private Point point = new Point();

    private SPUtils<DocumentBean> spUtils = new SPUtils<>();
    // 自定义控件 集合
    private RelativeLayout rlCustom;
    // 自定义View
    private CustomView mCustomView;
    //自定义View
    private XshapeView mXshapeView;
    // 自定义控件宽度
    private final int mViewWidth = 50;
    private final int margin = 20;
    // 状态栏高度
    private int statusBarHeight;
    // 悬浮框window参数
    private WindowManager.LayoutParams mLayoutParams;

    private Context mContext;
    // 窗口缩放视图
    private View aView;
    // 图片缩放动画
    private PathShrinkView mPathShrinkView;

    private WindowUtil() {

    }

    private static class SingletonInstance {
        @SuppressLint("StaticFieldLeak")
        private static final WindowUtil INSTANCE = new WindowUtil();
    }

    public static WindowUtil getInstance() {
        return SingletonInstance.INSTANCE;
    }

    public void showPermissionWindow(Context context, OnPermissionListener onPermissionListener) {
        if (RomUtils.checkFloatWindowPermission(context)) {
            showWindow(context);
        } else {
            if (onPermissionListener != null)
                onPermissionListener.showPermissionDialog();
        }
    }


    private IShowSuccess iShowSuccess;

    // 此方法，只能开启权限后才能调用
    public void showSuccessWindow(Context context, IShowSuccess iShowSuccess) {
        this.iShowSuccess = iShowSuccess;
        showWindow(context);
    }

    // 悬浮是否可见
    public void setCustomViewVisiable(int isVisiable) {
        if (rlCustom != null)
            rlCustom.setVisibility(isVisiable);
    }


    private View rootView;

    @SuppressLint("CheckResult")
    private void showWindow(Context context) {
        if (null == mWindowManager && null == rlCustom && null == aView) {
            mContext = context;
            mWindowManager = (WindowManager) context.getSystemService(WINDOW_SERVICE);
            mWindowManager.getDefaultDisplay().getSize(point);

            // 加载视图
            aView = LayoutInflater.from(mContext).inflate(R.layout.layout_article_window, null, false);
            mPathShrinkView = (PathShrinkView) aView.findViewById(R.id.pathShrinkView);
            aView.setVisibility(View.GONE);
            WindowManager.LayoutParams sLayoutParams = new WindowManager.LayoutParams();
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N_MR1) {
                sLayoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
            } else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.N_MR1) {
                sLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                sLayoutParams.type = WindowManager.LayoutParams.TYPE_TOAST;
            } else {
                sLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
            }
            sLayoutParams.format = PixelFormat.RGBA_8888;   //窗口透明
            sLayoutParams.gravity = Gravity.TOP | Gravity.LEFT;  //窗口位置
            sLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
            sLayoutParams.width = point.x;
            sLayoutParams.height = point.y;
            // 设置初始位置
            sLayoutParams.x = 0;
            sLayoutParams.y = 0;
            mWindowManager.addView(aView, sLayoutParams);

            // 悬浮框视图
            rlCustom = (RelativeLayout) LayoutInflater.from(mContext)
                    .inflate(R.layout.layout_article_costom, null, false);
            rlCustom.setId(R.id.rlCustumId);
            mCustomView = rlCustom.findViewById(R.id.customView);
            mXshapeView = rlCustom.findViewById(R.id.xshapeView);
            mLayoutParams = new WindowManager.LayoutParams();
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N_MR1) {
                mLayoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
            } else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.N_MR1) {
                mLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                mLayoutParams.type = WindowManager.LayoutParams.TYPE_TOAST;
            } else {
                mLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
            }
            mLayoutParams.format = PixelFormat.RGBA_8888;   //窗口透明
            mLayoutParams.gravity = Gravity.TOP | Gravity.LEFT;  //窗口位置
            mLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
            mLayoutParams.width = DisplayUtils.dip2px(context, mViewWidth);
            mLayoutParams.height = DisplayUtils.dip2px(context, mViewWidth);
            // 设置初始位置
            mLayoutParams.x = point.x - DisplayUtils.dip2px(context, margin);
            mLayoutParams.y = point.y - DisplayUtils.dip2px(context, 100);
            mWindowManager.addView(rlCustom, mLayoutParams);

            // 设置数据
            List<DocumentBean> datas = spUtils.getList(context, Contant.DOC_PATH, DocumentBean.class);
            setData(datas);
            initListener(context);
        }
        // 开启悬浮框成功监听
        if (iShowSuccess != null) {
            iShowSuccess.showSuccess();
        }
    }

    public void dismissWindow() {
        if (mWindowManager != null && rlCustom != null && aView != null) {
            if (rootView != null) {
                mWindowManager.removeViewImmediate(rootView);
                rootView = null;
            }
            mWindowManager.removeViewImmediate(aView);
            mWindowManager.removeViewImmediate(rlCustom);
            mWindowManager = null;
            rlCustom = null;
            aView = null;
        }
    }


    private void initListener(final Context context) {
        final int mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            statusBarHeight = context.getResources().getDimensionPixelSize(resourceId);
        }
        //设置触摸滑动事件
        rlCustom.setOnTouchListener(new View.OnTouchListener() {
            int startX, startY;  //起始点
            boolean isPerformClick;  //是否点击
            int finalMoveX;  //最后通过动画将mView的X轴坐标移动到finalMoveX

            @SuppressLint("ClickableViewAccessibility")
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Log.d("click", "onTouch: " + event.getAction());
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        startX = (int) event.getX();
                        startY = (int) event.getY();
                        isPerformClick = true;
                        // 收缩动画
                        shrinkViewXx();

                        return true;
                    case MotionEvent.ACTION_MOVE:
                        //判断是CLICK还是MOVE
                        if (Math.abs(startX - event.getX()) >= mTouchSlop || Math.abs(startY - event.getY()) >= mTouchSlop) {
                            isPerformClick = false;
                        }

                        mLayoutParams.x = (int) event.getRawX() - startX;
                        // 减去状态栏高度
                        mLayoutParams.y = (int) event.getRawY() - startY - statusBarHeight;
                        Log.e("TAG", "mLayoutParams.x---->" + mLayoutParams.x);
                        Log.e("TAG", "mLayoutParams.y---->" + mLayoutParams.y);
                        updateViewLayout();   //更新mView 的位置

                        return true;
                    case MotionEvent.ACTION_UP:
                        if (isPerformClick) {
                            rlCustom.performClick();
                        }
                        //判断mView是在Window中的位置，以中间为界
                        if (mLayoutParams.x + rlCustom.getMeasuredWidth() / 2 >= mWindowManager.getDefaultDisplay().getWidth() / 2) {
                            finalMoveX = mWindowManager.getDefaultDisplay().getWidth() - rlCustom.getMeasuredWidth();
                        } else {
                            finalMoveX = 0;
                        }
                        // 贴边动画
                        stickToSide();
                        return !isPerformClick;
                }
                return false;
            }

            /**
             * 自动贴边
             */
            private void stickToSide() {
                ValueAnimator animator = ValueAnimator.ofInt(mLayoutParams.x, finalMoveX).setDuration(Math.abs(mLayoutParams.x - finalMoveX));
                animator.setInterpolator(new AccelerateDecelerateInterpolator());
                animator.addUpdateListener(animation -> {
                    mLayoutParams.x = (int) animation.getAnimatedValue();
                    updateViewLayout();
                });
                animator.addListener(new Animator.AnimatorListener() {
                    @Override
                    public void onAnimationStart(Animator animator) {

                    }

                    @Override
                    public void onAnimationEnd(Animator animator) {
                        // 动画完成后，扩展动画
                        expandViewXx();
                    }

                    @Override
                    public void onAnimationCancel(Animator animator) {

                    }

                    @Override
                    public void onAnimationRepeat(Animator animator) {

                    }
                });
                animator.start();
            }
        });

        // 设置点击事件
        rlCustom.setOnClickListener(this);
    }

    //===================================== CustomView 触发动画逻辑===========================================

    /**
     * 扩展动画
     */
    private void expandViewXx() {
        if (mXshapeView == null) {
            return;
        }
        // 动画完成后，扩展CustomView
        if (mLayoutParams.x > mWindowManager.getDefaultDisplay().getWidth() / 2) {
            mXshapeView.setSideView(XshapeView.SIDE.RIGHT_SIDE_E);
        } else if (mLayoutParams.x < mWindowManager.getDefaultDisplay().getWidth() / 2) {
            mXshapeView.setSideView(XshapeView.SIDE.LEFT_SIDE_E);
        }
    }

    /**
     * 收缩动画
     */
    private void shrinkViewXx() {
        if (mXshapeView == null) {
            return;
        }
        // 动画完成后，扩展CustomView
        if (mLayoutParams.x > mWindowManager.getDefaultDisplay().getWidth() / 2) {
            mXshapeView.setSideView(XshapeView.SIDE.RIGHT_SIDE_S);
        } else if (mLayoutParams.x < mWindowManager.getDefaultDisplay().getWidth() / 2) {
            mXshapeView.setSideView(XshapeView.SIDE.LEFT_SIDE_S);
        }
    }

    //===================================== CustomView 结束动画===========================================


    private void updateViewLayout() {
        if (mWindowManager != null && null != rlCustom && null != mLayoutParams) {
            mWindowManager.updateViewLayout(rlCustom, mLayoutParams);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rlCustumId:
                showListView();
                rlCustom.setVisibility(View.GONE);
                break;
            case R.id.rl_list:
                setListOutAnim();
                animationOut.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {
                        if (rootView != null) {
                            RelativeLayout rLayout = rootView.findViewById(R.id.rl_list);
                            rLayout.startAnimation(animationAlpha1To0);
                        }
                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        if (mWindowManager != null && rootView != null) {
                            mWindowManager.removeView(rootView);
                            rootView = null;
                        }
                        if (rlCustom != null)
                            rlCustom.setVisibility(View.VISIBLE);
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });
                int count = listView.getChildCount();
                for (int i = 0; i < count; i++) {
                    // 先移除动画
                    listView.getChildAt(i).clearAnimation();
                    listView.getChildAt(i).startAnimation(animationOut);
                }
                break;
        }
    }


    // 方位
    public static final int DIRECTION_LEFT_TOP = 1,
            DIRECTION_LEFT_BOTTOM = 2,
            DIRECTION_RIGHT_TOP = 3,
            DIRECTION_RIGHT_BOTTOM = 4;
    // 悬浮框 相对屏幕的位置
    private int mDirection;
    ListView listView;
    // 整个悬浮框的数据源
    List<DocumentBean> mLists = new ArrayList<>();
    // 适配器
    DocumentAdapter adapter;

    private void showListView() {
        //获取customView控件在屏幕的位置
        int[] location = new int[2];
        rlCustom.getLocationOnScreen(location);
        Log.e(TAG, "location-->X=" + location[0]);
        Log.e(TAG, "location-->Y=" + location[1]);
        int width = rlCustom.getMeasuredWidth();
        int height = rlCustom.getMeasuredHeight();
        if (location[0] + width / 2 >= DisplayUtils.getDisplayWidth(mContext) / 2) {
            if (location[1] + height / 2 >= DisplayUtils.getDisplayHeight(mContext) / 2) {
                mDirection = DIRECTION_RIGHT_BOTTOM;
            } else {
                mDirection = DIRECTION_RIGHT_TOP;
            }
        } else {
            if (location[1] + height / 2 >= DisplayUtils.getDisplayHeight(mContext) / 2) {
                mDirection = DIRECTION_LEFT_BOTTOM;
            } else {
                mDirection = DIRECTION_LEFT_TOP;
            }
        }

        rootView = LayoutInflater.from(mContext).inflate(R.layout.layout_more_popwin_list, null);
        RelativeLayout rLayout = rootView.findViewById(R.id.rl_list);
        listView = (ListView) rootView.findViewById(R.id.listView);

        // 计算listView 在父控件中位置
        RelativeLayout.LayoutParams listParams = new RelativeLayout.LayoutParams(2 * DisplayUtils.getDisplayWidth(mContext) / 3,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        int margin = DisplayUtils.dip2px(mContext, 40);
        // 右上  添加ListView在 mCustomView 右下
        if (mDirection == DIRECTION_RIGHT_TOP) {
            listParams.topMargin = location[1] + height + margin;
            listParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
            listParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
        } else if (mDirection == DIRECTION_RIGHT_BOTTOM) {
            listParams.bottomMargin = DisplayUtils.getDisplayHeight(mContext) - location[1] + margin;
            listParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
            listParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
        } else if (mDirection == DIRECTION_LEFT_TOP) {
            listParams.topMargin = location[1] + height + margin;
            listParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
            listParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
        } else if (mDirection == DIRECTION_LEFT_BOTTOM) {
            listParams.bottomMargin = DisplayUtils.getDisplayHeight(mContext) - location[1] + margin;
            listParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
            listParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
        }
        listView.setLayoutParams(listParams);

        // 透明度动画
        animationAlpha0To1 = AnimationUtils.loadAnimation(mContext, R.anim.layout_alpha_0to1);
        animationAlpha1To0 = AnimationUtils.loadAnimation(mContext, R.anim.layout_alpha_1to0);

        // item进场动画
        listView.clearAnimation();
        Animation animationIn = null;
        // 设置Item进场动画
        if (mDirection == DIRECTION_RIGHT_TOP || mDirection == DIRECTION_RIGHT_BOTTOM) {
            animationIn = AnimationUtils.loadAnimation(mContext, R.anim.list_set_anim_r_in);
        } else if (mDirection == DIRECTION_LEFT_TOP || mDirection == DIRECTION_LEFT_BOTTOM) {
            animationIn = AnimationUtils.loadAnimation(mContext, R.anim.list_set_anim_l_in);
        }
        LayoutAnimationController controller = new LayoutAnimationController(animationIn);
        controller.setDelay(0.5f);
        controller.setOrder(LayoutAnimationController.ORDER_NORMAL);// 普通队列模式
        listView.setLayoutAnimation(controller);
        animationIn.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                rLayout.startAnimation(animationAlpha0To1);
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                listView.clearAnimation();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

        WindowManager.LayoutParams xLayoutParams = new WindowManager.LayoutParams();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            xLayoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        } else {
            xLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
        }
        xLayoutParams.format = PixelFormat.RGBA_8888;   //窗口透明
        xLayoutParams.gravity = Gravity.TOP | Gravity.LEFT;  //窗口位置
        xLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        xLayoutParams.alpha = 1.0f;  //在0.0f到1.0f之间。1.0完全不透明，0.0f完全透明
        xLayoutParams.width = point.x;
        xLayoutParams.height = point.y;

        // 设置初始位置
        xLayoutParams.x = 0;
        xLayoutParams.y = 0;
        mWindowManager.addView(rootView, xLayoutParams);

        adapter = new DocumentAdapter(mContext, mLists, mDirection);
        adapter.setIDeleteItem(this);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);
        rLayout.setOnClickListener(this);
    }


    /**
     * 供内部使用
     *
     * @param lists
     */
    private void setData(List<DocumentBean> lists) {
        if (lists == null) {
            return;
        }
        // 数据为空,则关闭悬浮框(内部使用，只有删除功能，则不需要showWindow(context))
        if (lists.size() == 0) {
            setCustomViewVisiable(View.GONE);
            return;
        }
        mLists.clear();
        mLists.addAll(lists);

        int[] resouces = new int[lists.size()];
        for (int i = 0; i < lists.size(); i++) {
            DocumentBean documentBean = lists.get(i);
            resouces[i] = documentBean.getThumb();
        }
        if (mCustomView != null)
            mCustomView.setResouces(resouces);
    }

    /**
     * 供外部使用
     *
     * @param lists
     */
    public void setDataOut(List<DocumentBean> lists) {
        if (lists == null) {
            return;
        }
        // 数据为空,则关闭悬浮框
        if (lists.size() == 0) {
            setCustomViewVisiable(View.GONE);
            return;
        } else {
            setCustomViewVisiable(View.VISIBLE);
        }

        mLists.clear();
        mLists.addAll(lists);
        int[] resouces = new int[lists.size()];
        for (int i = 0; i < lists.size(); i++) {
            DocumentBean documentBean = lists.get(i);
            resouces[i] = documentBean.getThumb();
        }
        if (mCustomView != null)
            mCustomView.setResouces(resouces);
    }


    private boolean isAnimIng;

    /**
     * 设置窗口背景  和  窗口缩小动画
     */
    public void startScaleAnim() {
        if (!SharedPreUtils
                .getBoolean(mContext, Contant.IS_EXECUTE_ANIM)) {
            return;
        }
        // 如果正在动画
        if (isAnimIng) {
            return;
        }
        isAnimIng = true;
        //获取customView控件在屏幕的位置
        int[] location = new int[2];
        rlCustom.getLocationOnScreen(location);
        int width = rlCustom.getMeasuredWidth();
        int height = rlCustom.getMeasuredHeight();
        Point point = new Point(location[0], location[1]);
        aView.setVisibility(View.VISIBLE);
        mPathShrinkView.setCenterXY(point, Math.max(width / 2, height / 2));
        // 开始缩小动画
        mPathShrinkView.startAnim(new PathShrinkView.IFinishValueAnim() {
            @Override
            public void finishAnim() {
                SharedPreUtils.saveBoolean(mContext, Contant.IS_EXECUTE_ANIM, false);
                isAnimIng = false;
                aView.setVisibility(View.GONE);
            }
        });
    }


    /**
     * 列表点击
     *
     * @param adapterView
     * @param view
     * @param position
     * @param l
     */
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
        if (Utils.isFastClick()) {  // 防止快速点击
            setListOutAnim();
            LayoutAnimationController controller = new LayoutAnimationController(animationOut);
            controller.setDelay(0.5f);
            controller.setOrder(LayoutAnimationController.ORDER_NORMAL);// 普通队列模式
            listView.setLayoutAnimation(controller);
            animationOut.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                    if (rootView != null) {
                        RelativeLayout rLayout = rootView.findViewById(R.id.rl_list);
                        rLayout.startAnimation(animationAlpha1To0);
                    }
                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    if (mWindowManager != null && rootView != null) {
                        mWindowManager.removeView(rootView);
                        rootView = null;
                    }
                    Adapter adapter = adapterView.getAdapter();
                    DocumentBean documentBean = (DocumentBean) adapter.getItem(position);
                    iWinCallback.onCall(documentBean);
                    listView.clearAnimation();
                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });
        }
    }

    /**
     * 删除Item执行动画
     */
    @Override
    public void deleteAnim(int position) {
        if (Utils.isFastClick()) {  // 防止快速点击
            setListOutAnim();
            animationOut.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                    if (rootView != null) {
                        RelativeLayout rLayout = rootView.findViewById(R.id.rl_list);
                        rLayout.startAnimation(animationAlpha1To0);
                    }
                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    List<DocumentBean> lists = new ArrayList<>();
                    lists.addAll(mLists);
                    lists.remove(position); // 移除值
                    spUtils.saveList(mContext, Contant.DOC_PATH, lists);

                    // 列表为空，则移除视图
                    if (lists.size() == 0) {
                        if (mWindowManager != null && rootView != null) {
                            mWindowManager.removeView(rootView);
                            rootView = null;
                        }
                    } else {
                        // 重新填充数据，刷新adapter
                        setData(lists);
                        adapter.notifyDataSetChanged();
                    }
                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });
            // 先清除动画
            listView.getChildAt(position).clearAnimation();
            listView.getChildAt(position).startAnimation(animationOut);
        }
    }

    //  退场动画
    Animation animationOut = null;
    // 透明度动画
    Animation animationAlpha0To1 = null;
    Animation animationAlpha1To0 = null;

    /**
     * 设置listView 退场动画
     */
    private void setListOutAnim() {
        if (mDirection == DIRECTION_RIGHT_TOP || mDirection == DIRECTION_RIGHT_BOTTOM) {
            animationOut = AnimationUtils.loadAnimation(mContext, R.anim.list_set_anim_r_out);
        } else if (mDirection == DIRECTION_LEFT_TOP || mDirection == DIRECTION_LEFT_BOTTOM) {
            animationOut = AnimationUtils.loadAnimation(mContext, R.anim.list_set_anim_l_out);
        }
    }


    interface OnPermissionListener {
        void showPermissionDialog();
    }

    // 设置跳转回调
    public void setIWinCallback(IWinCallback iWinCallback) {
        this.iWinCallback = iWinCallback;
    }


}